package client;

import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh;
import com.jme3.effect.shapes.EmitterBoxShape;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;

/**
 *
 * @author Ben
 */
public class ParticleSystem
{
    static long time;
    static long lasttime;
    static String name;
    ParticleEmitter newC;
    
    public void start()
    {
        newC.emitAllParticles();
    }

    public void end()
    {
        newC.killAllParticles();
    }

    public void kill()
    {
        newC.removeFromParent();
    }
    
    public String getName()
    {
        return name;
    }
    
    public static class CustomParticle extends ParticleSystem
    {
        String texture;
        ColorRGBA color;
        Vector3f pos;
        boolean oneshot;
        
        public CustomParticle(String part, ColorRGBA col, String na, Vector3f po, boolean one)
        {
            texture = part;
            color = col;
            name = na;
            pos = po;
            oneshot = one;
        }
        
        public void create()
        {
            newC = new ParticleEmitter("em:"+name,ParticleMesh.Type.Triangle,180);
            Material particle_skin = new Material(Main.appref.getAssetManager(),"Common/MatDefs/Misc/Particle.j3md");
            particle_skin.setTexture("Texture", Main.appref.getAssetManager().loadTexture(texture));
            newC.setMaterial(particle_skin);
            newC.setImagesX(3); newC.setImagesY(3);
            newC.setEndColor(color);
            newC.setStartColor(color.mult(2));
            newC.getParticleInfluencer().setInitialVelocity(new Vector3f(0,50,0));
            newC.setStartSize(3f);
            newC.setEndSize(0.1f);
            newC.setGravity(10,35,10);
            newC.setLowLife(1f);
            newC.setHighLife(1.5f);
            newC.getParticleInfluencer().setVelocityVariation(0.3f);
            Main.rendernode.attachChild(newC);
            newC.setLocalTranslation(pos);
            if(oneshot)
            {
                ControlDestroy d = new ControlDestroy(1000L);
                newC.addControl(d);
            }
        }
    }
    
    public static class ParticleFireSmall extends ParticleSystem
    {
        ParticleEmitter newB;
        String texture = "Textures/fireparticle.png";
        ColorRGBA color;
        Vector3f pos;
        
        public ParticleFireSmall(String namei, Vector3f posi, ColorRGBA col)
        {
            name = namei;
            pos = posi;
            color = col;
        }
        
        public void create()
        {
            newC = new ParticleEmitter("em:"+name,ParticleMesh.Type.Triangle,300);
            newB = new ParticleEmitter("em2:"+name,ParticleMesh.Type.Triangle,100);
            Material particle_skin = new Material(Main.appref.getAssetManager(),"Common/MatDefs/Misc/Particle.j3md");
            particle_skin.setTexture("Texture", Main.appref.getAssetManager().loadTexture(texture));
            Material particle_skin2 = new Material(Main.appref.getAssetManager(),"Common/MatDefs/Misc/Particle.j3md");
            particle_skin2.setTexture("Texture", Main.appref.getAssetManager().loadTexture(texture));
            particle_skin2.setBoolean("m_PointSprite", true);
            newC.setMaterial(particle_skin);
            newB.setMaterial(particle_skin2);
            newC.setImagesX(2); newC.setImagesY(2);
            newB.setImagesX(2); newB.setImagesY(2);
            newC.setEndColor(color);
            newB.setEndColor(ColorRGBA.DarkGray);
            newC.setStartColor(color);
            newB.setStartColor(ColorRGBA.DarkGray);
            newC.getParticleInfluencer().setInitialVelocity(new Vector3f(0,1,0));
            newB.getParticleInfluencer().setInitialVelocity(new Vector3f(0,1,0));
            //newC.addLight(Main.lights.playerlight);
            //newB.addLight(Main.lights.playerlight);
            newC.setStartSize(1.5f);
            newB.setStartSize(1.25f);
            newC.setEndSize(0.1f);
            newB.setEndSize(0.1f);
            newC.setGravity(0,-25,0);
            newB.setGravity(0f, -10f, 0f);
            newC.setLowLife(0.45f);
            newB.setLowLife(0.10f);
            newC.setHighLife(0.85f);
            newB.setHighLife(0.75f);
            newC.getParticleInfluencer().setVelocityVariation(0.25f);
            newB.getParticleInfluencer().setVelocityVariation(0.45f);
            Main.rendernode.attachChild(newC);
            Main.rendernode.attachChild(newB);
            newC.setLocalTranslation(pos);
            newB.setLocalTranslation(pos);
        }
    }
    
    public static class ParticleFireLarge extends ParticleSystem
    {
        ParticleEmitter newB;
        String texture = "Textures/fireparticle.png";
        String texture_2 = "Textures/smokeparticle.png";
        ColorRGBA color;
        Vector3f pos;
        
        public ParticleFireLarge(String namei, Vector3f posi, ColorRGBA col)
        {
            name = namei;
            pos = posi;
            color = col;
        }
        
        public void create()
        {
            newC = new ParticleEmitter("em:"+name,ParticleMesh.Type.Triangle,300);
            newB = new ParticleEmitter("em2:"+name,ParticleMesh.Type.Triangle,100);
            Material particle_skin = new Material(Main.appref.getAssetManager(),"Common/MatDefs/Misc/Particle.j3md");
            particle_skin.setTexture("Texture", Main.appref.getAssetManager().loadTexture(texture));
            Material particle_skin2 = new Material(Main.appref.getAssetManager(),"Common/MatDefs/Misc/Particle.j3md");
            particle_skin2.setTexture("Texture", Main.appref.getAssetManager().loadTexture(texture_2));
            particle_skin2.setBoolean("m_PointSprite", true);
            newC.setMaterial(particle_skin);
            newB.setMaterial(particle_skin2);
            newC.setImagesX(2); newC.setImagesY(2);
            newB.setImagesX(2); newB.setImagesY(2);
            newC.setEndColor(color);
            newB.setEndColor(ColorRGBA.DarkGray);
            newC.setStartColor(color);
            newB.setStartColor(ColorRGBA.DarkGray);
            newC.setParticlesPerSec(60f);
            newC.setRotateSpeed(1f);
            newC.getParticleInfluencer().setInitialVelocity(new Vector3f(0.9f,0.9f,0.9f));
            newC.setShape(new EmitterBoxShape(new Vector3f(1,-1,1),new Vector3f(4,-1,4)));
            newB.getParticleInfluencer().setInitialVelocity(new Vector3f(0.2f,0.2f,0.2f));
            newB.setShape(new EmitterBoxShape(new Vector3f(1,-1,1),new Vector3f(4,-1,4)));
            newC.addLight(Main.lights.playerlight);
            //newB.addLight(Main.lights.playerlight);
            newC.setStartSize(3f);
            newB.setStartSize(2.5f);
            newC.setEndSize(0.05f);
            newB.setEndSize(0.25f);
            newC.setGravity(0,-25,0);
            newB.setGravity(0f, -10f, 0f);
            newC.setLowLife(0.10f);
            newB.setLowLife(0.75f);
            newC.setHighLife(1.0f);
            newB.setHighLife(2.0f);
            newC.getParticleInfluencer().setVelocityVariation(0f);
            newB.getParticleInfluencer().setVelocityVariation(1.5f);
            Main.rendernode.attachChild(newC);
            Main.rendernode.attachChild(newB);
            newC.setLocalTranslation(pos.subtract(new Vector3f(3,-1,3)));
            newB.setLocalTranslation(pos.subtract(new Vector3f(3,-1,3)));
        }
    }
    
    public static class DustPuff extends ParticleSystem
    {
        String texture = "Textures/dustparticle.png";
        ColorRGBA color;
        Vector3f pos;
        
        public DustPuff(String namei, Vector3f posi, ColorRGBA col)
        {
            name = namei;
            pos = posi;
            color = col;
        }
        
        public void create()
        {
            newC = new ParticleEmitter("em:"+name,ParticleMesh.Type.Triangle,150);
            Material particle_skin = new Material(Main.appref.getAssetManager(),"Common/MatDefs/Misc/Particle.j3md");
            particle_skin.setTexture("Texture", Main.appref.getAssetManager().loadTexture(texture));
            newC.setMaterial(particle_skin);
            newC.setImagesX(1); newC.setImagesY(1);
            newC.setEndColor(color);
            newC.setStartColor(color);
            newC.setParticlesPerSec(0f);
            newC.setRotateSpeed(1f);
            newC.getParticleInfluencer().setInitialVelocity(new Vector3f(0f,2.5f,0f));
            //newC.addLight(Main.lights.playerlight);
            newC.setStartSize(1.5f);
            newC.setEndSize(0.05f);
            newC.setGravity(0,10,0);
            newC.setLowLife(0.10f);
            newC.setHighLife(1.0f);
            newC.getParticleInfluencer().setVelocityVariation(5f);
            Main.rendernode.attachChild(newC);
            newC.setLocalTranslation(pos);
            ControlDestroy d = new ControlDestroy(1000L);
            newC.addControl(d);
        }
    }
}
